/**
 * @fileOverview The "sourcearea" plugin. It registers the "source" editing
 *      mode, which displays the raw data being edited in the editor.
 */
(function() {
   VED.plugins.add( 'sourcearea', {
      lang: 'en,ru', // %REMOVE_LINE_CORE%
      icons: 'source,source-rtl', // %REMOVE_LINE_CORE%
      init: function( editor ) {
         // Source mode isn't available in inline mode yet.
         if ( editor.elementMode == VED.ELEMENT_MODE_INLINE )
            return;

         var sourcearea = VED.plugins.sourcearea;

         editor.addMode( 'source', function( callback ) {
            var contentsSpace = editor.ui.space( 'contents' ),
               textarea = contentsSpace.getDocument().createElement( 'textarea' );

            textarea.setStyles(
               VED.tools.extend({
                  // IE7 has overflow the <textarea> from wrapping table cell.
                  width: VED.env.ie7Compat ? '99%' : '100%',
                  height: '100%',
                  resize: 'none',
                  outline: 'none',
                  'text-align': 'left'
               },
               VED.tools.cssVendorPrefix( 'tab-size', editor.config.sourceAreaTabSize || 4 ) ) );

            // Make sure that source code is always displayed LTR,
            // regardless of editor language (#10105).
            textarea.setAttribute( 'dir', 'ltr' );

            textarea.addClass( 'ved_source ved_reset ved_enable_context_menu' );

            editor.ui.space( 'contents' ).append( textarea );

            var editable = editor.editable( new sourceEditable( editor, textarea ) );

            // Fill the textarea with the current editor data.
            editable.setData( editor.getData( 1 ) );

            // Having to make <textarea> fixed sized to conquer the following bugs:
            // 1. The textarea height/width='100%' doesn't constraint to the 'td' in IE6/7.
            // 2. Unexpected vertical-scrolling behavior happens whenever focus is moving out of editor
            // if text content within it has overflowed. (#4762)
            if ( VED.env.ie ) {
               editable.attachListener( editor, 'resize', onResize, editable );
               editable.attachListener( VED.document.getWindow(), 'resize', onResize, editable );
               VED.tools.setTimeout( onResize, 0, editable );
            }

            editor.fire( 'ariaWidget', this );

            callback();
         });

         editor.addCommand( 'source', sourcearea.commands.source );

         if ( editor.ui.addButton ) {
            editor.ui.addButton( 'Source', {
               label: editor.lang.sourcearea.toolbar,
               command: 'source',
               toolbar: 'mode,10'
            });
         }

         editor.on( 'mode', function() {
            editor.getCommand( 'source' ).setState( editor.mode == 'source' ? VED.TRISTATE_ON : VED.TRISTATE_OFF );
         });

         function onResize() {
            // Holder rectange size is stretched by textarea,
            // so hide it just for a moment.
            this.hide();
            this.setStyle( 'height', this.getParent().$.clientHeight + 'px' );
            this.setStyle( 'width', this.getParent().$.clientWidth + 'px' );
            // When we have proper holder size, show textarea again.
            this.show();
         }
      }
   });

   var sourceEditable = VED.editable.extend({
      setData: function( data ) {
         this.setValue( data );
         this.editor.fire( 'dataReady' );
      },

      getData: function() {
         return this.getValue();
      },

      // Insertions are not supported in source editable.
      insertHtml: function() {},
      insertElement: function() {},
      insertText: function() {},

      // Read-only support for textarea.
      setReadOnly: function( isReadOnly ) {
         this[ ( isReadOnly ? 'set' : 'remove' ) + 'Attribute' ]( 'readOnly', 'readonly' );
      },

      detach: function() {
         this.parent.call( this );
         this.clearCustomData();
         this.remove();
      }
   });
})();

VED.plugins.sourcearea = {
   commands: {
      source: {
         modes: { wysiwyg:1,source:1 },
         editorFocus: false,
         readOnly: 1,
         exec: function( editor ) {
            if ( editor.mode == 'wysiwyg' )
               editor.fire( 'saveSnapshot' );
            editor.getCommand( 'source' ).setState( VED.TRISTATE_DISABLED );
            editor.setMode( editor.mode == 'source' ? 'wysiwyg' : 'source' );
         },

         canUndo: false
      }
   }
};






